package ipdesk.model.report.channel.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.template.SqlMapDaoTemplate;
import com.opensymphony.xwork2.ActionContext;

import ecg.util.SessionUtils;

import ipdesk.model.report.channel.bean.ChannelInfoBean;


public class ChannelInfoImpl extends SqlMapDaoTemplate implements IChannelInfo
{
	
	public ChannelInfoImpl(DaoManager manager)
	{
		super(manager);	
	}
	
	public int channelInfoReg(ChannelInfoBean channelInfoBean) throws Exception
	{
		int channelCodeCount = 0;
		int lastChannelSeq = 0;
		String channelCode = "";
		
		String channelDate = channelInfoBean.getChannelDate();
		String deskCode = channelInfoBean.getDeskCode();
		
		// 관리코드를 생성하기 위해 등록날짜와 데스크 코드를 조건으로 한 데이터의 갯수를 가지고온다.
		channelCodeCount = ((Integer)this.queryForObject("channel.channelCode-count", channelInfoBean)).intValue();
				
		String endNumber = StringUtils.leftPad(Integer.toString(channelCodeCount+1), 4, "0");
		
		// 생성된 관리코드.
		channelCode = channelDate.replaceAll("-","") + "-" + deskCode.substring(4) + "-CHN-" + endNumber;
		
		
		
		// 생성된 침해조사일지 관리코드 빈 객체에 저장.
		channelInfoBean.setManageNo(channelCode);
		
		channelInfoBean.setUserSeq(Integer.parseInt(SessionUtils.getLoginSession().get("userSeq")));
		
		// iBatis 연동. 데이터 insert.
		lastChannelSeq = ((Integer)this.insert("channel.channelInfoInsert", channelInfoBean)).intValue();
				

		return lastChannelSeq;
	}
	
	public void insertFileData(ChannelInfoBean channelInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("channel.channelFileInfoInsert", channelInfoBean);
	}
	
	public int getDataTotalCount(ChannelInfoBean channelInfoBean) throws Exception
	{
		System.out.println(" :::: "+SessionUtils.getLoginSession().get("authCode"));

		
		System.out.println("totalCount ::: deskCode Session :: "+SessionUtils.getLoginSession().get("deskCode"));
		Map map = new HashMap();
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		//if(channelInfoBean.getDeskCode() == null || "".equals(channelInfoBean.getDeskCode())){
		//	map.put("deskCode", SessionUtils.getLoginSession().get("deskCode"));
		//}else{
			map.put("deskCode", channelInfoBean.getDeskCode());
		//}
		
		//channelInfoBean.setDeskCode(SessionUtils.getLoginSession().get("deskCode"));	


		int totalDataCount = ((Integer)this.queryForObject("channel.channelInfoTotalcount", map)).intValue();

		return totalDataCount;
	}
	
	public List<ChannelInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		map.put("userSeq", SessionUtils.getLoginSession().get("userSeq"));
		map.put("authCode", SessionUtils.getLoginSession().get("authCode"));
		System.out.println(map.get("startRow"));
		System.out.println(map.get("limitRowCount"));
		List<ChannelInfoBean> pageDataList = this.queryForList("channel.channelPageDataListForUser", map);
		
		return pageDataList;
	}
	
	public ChannelInfoBean getDetailData(int channelSeq){
		ChannelInfoBean channelInfoBean = (ChannelInfoBean)this.queryForObject("channel.channelDetailData", channelSeq);
		return channelInfoBean;
	}
	
	public void channelInfoUpdate(ChannelInfoBean channelInfoBean)
	{
		this.update("channel.channelInfoUpdate", channelInfoBean);
	}
	
	public void insertFileData2(ChannelInfoBean channelInfoBean)
	{
		// iBatis 연동. 데이터 insert.
		this.insert("channel.channelFileInfoInsert2", channelInfoBean);
	}
	
	public void delFileData(String fileSeq)
	{
		this.delete("channel.channelDelFileData", fileSeq);
	}
	
	public void delDetailData(int channelSeq)
	{
		this.update("channel.channelDelDetailData", channelSeq);
	}
}
